package com.controller.sys;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.base.BaseController;
import com.entity.sys.SysDict;
import com.service.sys.SysDictService;
import fuhua.com.anno.CheckPermission;
import fuhua.com.anno.Log;
import fuhua.com.util.ResultUtil;
import fuhua.com.util.TreeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 字典
 */
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends BaseController<SysDictService,SysDict> {

    /**
     * 字典类型树形列表
     */
    @GetMapping("/dictTypeList")
    @CheckPermission(perm = "system:dict:view")
    public ResultUtil dictTypeList(@RequestParam Map<String,String> map) {
        List<SysDict> list = service.dictTypeList(map);
        return ResultUtil.success(TreeUtil.buildTreeIsRoot(list,"0"));
    }

    /**
     * 字典项列表
     */
    @GetMapping("/page")
    @CheckPermission(perm = "system:dict:view")
    public ResultUtil page(@RequestParam Map<String,String> map) {
        return ResultUtil.success(service.page(map));
    }

    /**
     * 根据id获取信息
     */
    @GetMapping("/getById/{id}")
    public ResultUtil getById(@PathVariable("id") String id) {
        return ResultUtil.success(service.getById(id));
    }

    /**
     * 生成字典编码
     */
    @Log(title = "生成字典编码")
    @GetMapping("/createDictCode")
    public ResultUtil createDictCode(String dictName) {
        String dictCode = PinyinUtil.getFirstLetter(dictName,"").toUpperCase();
        return ResultUtil.success(dictCode);
    }

    /**
     * 判断字典名称是否重复
     */
    @GetMapping("/hasDictByName")
    public ResultUtil hasDictByName(String dictName,String id){
        boolean flag = service.hasDictByNameAndCode(dictName, null,id);
        Map<String,Object> map = new HashMap<>();
        map.put("isRepeat",flag);
        if (flag){
            dictName = service.createName(dictName);
            String dictCode = PinyinUtil.getFirstLetter(dictName,"").toUpperCase();
            map.put("dictName",dictName);
            map.put("dictCode",dictCode);
        }
        return ResultUtil.success(map);
    }

    /**
     * 判断字典代码是否重复
     */
    @GetMapping("/hasDictByCode")
    public ResultUtil hasDictByCode(String dictCode,String id){
        boolean flag = service.hasDictByNameAndCode(null, dictCode,id);
        Map<String,Object> map = new HashMap<>();
        map.put("isRepeat",flag);
        if (flag){
            map.put("dictCode",service.createCode(dictCode));
        }
        return ResultUtil.success(map);
    }

    /**
     * 新增
     */
    @Log(title = "新增字典")
    @PostMapping("/insert")
    @CheckPermission(perm = "system:dict:add")
    public ResultUtil insert(@RequestBody SysDict dict) {
        if (service.hasDictByNameAndCode(dict.getName(), null,dict.getId())){
            String name = service.createName(dict.getName());
            dict.setName(name);
        }
        if (service.hasDictByNameAndCode(null, dict.getCode(), dict.getId())){
            String code = service.createCode(dict.getCode());
            dict.setCode(code);
        }
        dict.setId(IdUtil.fastSimpleUUID());
        service.save(dict);
        return ResultUtil.success();
    }

    /**
     * 修改
     */
    @Log(title = "修改字典")
    @PostMapping("/update")
    @CheckPermission(perm = "system:dict:update")
    public ResultUtil update(@RequestBody SysDict dict) {
        if (service.hasDictByNameAndCode(dict.getName(), null,dict.getId())){
            String name = service.createName(dict.getName());
            dict.setName(name);
        }
        if (service.hasDictByNameAndCode(null, dict.getCode(), dict.getId())){
            String code = service.createCode(dict.getCode());
            dict.setCode(code);
        }
        service.updateById(dict);
        return ResultUtil.success();
    }

    /**
     * 删除
     */
    @Log(title = "删除字典")
    @DeleteMapping("/delete/{id}")
    @CheckPermission(perm = "system:dict:delete")
    public ResultUtil delete(@PathVariable("id") String id) {
        SysDict byId = service.getById(id);
        if (byId != null && byId.getIsType()==1){
            if (service.hasDictByParent(byId.getId())){
                return ResultUtil.error("删除失败，该字典类型还存在子级");
            }
        }
        String [] ids = Convert.toStrArray(id);
        service.removeByIds(Arrays.asList(ids));
        return ResultUtil.success();
    }

    /**
     * 根据字典类型代码查询字典项列表
     */
    @GetMapping("/getByTypeCode")
    public ResultUtil getByTypeCode(String dictCode) {
        SysDict one = service.getByDictCode(dictCode);
        if (Objects.nonNull(one)){
            QueryWrapper<SysDict> query = new QueryWrapper<>();
            query.eq("parent_id",one.getId());
            query.orderByAsc("sort");
            return ResultUtil.success(service.list(query));
        }
        return ResultUtil.success();
    }
}
